package dao.entity;

import dao.MysqlConfiguration;
import dao.interf.Manager;
import dao.util.DaoTools;
import tools.TimeProcess;

import java.sql.*;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

public class ServiceDaoImpl implements Manager {
    public static int sidConf=10000;
    public boolean serviceLogin(String sname,String pwd) {
        Connection connection= MysqlConfiguration.MysqlConnection();
        String sql="select * from services where sname=? and pwd=?";
        ResultSet rs=null;
        try {
            PreparedStatement pst=connection.prepareStatement(sql);
            pst.setString(1,sname);
            pst.setString(2,pwd);
            rs=pst.executeQuery();
        } catch (SQLException e) {
//            throw new RuntimeException(e);
            System.out.println("创建select客服语句对象失败:"+e.getMessage());
        }
        boolean tag=false;
        try {
            if (rs != null) {
                tag=rs.next();
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        if(tag){
            //登录成功，要及时更新登录状态和登录时间
            String lg_time= TimeProcess.getCurrentTime();//当前时间
            String sql_update="update services set lg_time=?,s_status=1 where sname=?";
            try {
                PreparedStatement pst_update=connection.prepareStatement(sql_update);
                pst_update.setString(1,lg_time);
                pst_update.setString(2,sname);
                pst_update.executeUpdate();
            } catch (SQLException e) {
//                throw new RuntimeException(e);
                System.out.println("创建update services客服语句对象失败:"+e.getMessage());
            }
        }
        return tag;
    }

    public int addService(String sname,String pwd) {
        Connection connection= MysqlConfiguration.MysqlConnection();
        String sql="insert into services values(?,?,?,?,?,?)";
        System.out.println(sql);
        int n=0;//受影响的行数
        try {
            PreparedStatement pst=connection.prepareStatement(sql);
            System.out.println("创建insert顾客语句对象成功!");
            sidConf+= DaoTools.countPersonSum("services") +1;
            pst.setString(1,sidConf+"");
            pst.setString(2,sname);
            pst.setString(3,pwd);
            String lg_time=TimeProcess.getCurrentTime();
            pst.setString(4,lg_time);
            pst.setInt(5,1);
            pst.setInt(6,1);
            n=pst.executeUpdate();
        } catch (SQLException e) {
            System.out.println("创建insert客服语句对象失败:"+e.getMessage());
        }
        return n;
    }
    @Override
    public ResultSet selectManagerByName(String sname) {
        Connection connection=MysqlConfiguration.MysqlConnection();
        String sql="select * from services where sname=?";
        System.out.println(sql);
        ResultSet rs=null;
        try {
            PreparedStatement pst=connection.prepareStatement(sql);
            pst.setString(1,sname);
            rs= pst.executeQuery();
        } catch (SQLException e) {
            System.out.println("创建select客服语句对象失败:"+e.getMessage());
        }
        return rs;
    }
    @Override
    public int logOut(String sname) {
        Connection connection= MysqlConfiguration.MysqlConnection();
        String sql="update services set s_status=0 where sname=?";
        int n=0;
        try {
            PreparedStatement pst=connection.prepareStatement(sql);
            pst.setString(1,sname);
            n=pst.executeUpdate();
        } catch (SQLException e) {
//            throw new RuntimeException(e);
            System.out.println("创建update service客服对象失败:"+e.getMessage());
        }
        return n;
    }
    @Override
    public boolean isOnline(String sname) {
        Connection connection=MysqlConfiguration.MysqlConnection();
        String sql="select s_status from services where sname=?";
        ResultSet rs=null;
        boolean tag=false;
        try {
            PreparedStatement pst=connection.prepareStatement(sql);
            pst.setString(1,sname);
            rs=pst.executeQuery();
        } catch (SQLException e) {
            System.out.println("创建select s_status客服语句对象失败:"+e.getMessage());
        }
        try {
            rs.next();
            tag=rs.getBoolean("s_status");
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return tag;
    }
}
